package com.ysm.smfu.adapter;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.FoundActivity;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;

/**
 * Created by Administrator on 2016/11/15.
 */
public class BannerPagerAdapter extends PagerAdapter {

    private String[] links;
    private Fragment fragment;
    private String[] ids;
    private String[] drawers;
    private int[] drawers_noData;
    boolean hasAct; //是否有活动

    public BannerPagerAdapter(String[] drawers, String[] links, String[] ids, Fragment fragment) {
        this.drawers = drawers;
        this.links = links;
        this.ids = ids;
        this.fragment = fragment;
        hasAct = true;
    }

    /**
     * 默认显示
     *
     * @param drawers_noData
     */
    public BannerPagerAdapter(int[] drawers_noData) {
        this.drawers_noData = drawers_noData;
    }

    @Override
    public int getCount() {
        return drawers == null ? drawers_noData.length : drawers.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = UIUtils.inflate(R.layout.item_grid_banner);
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
//        ImageView imageView = new ImageView(UIUtils.getContext());
        if (hasAct) {
            if (!TextUtils.isEmpty(drawers[position]) && !drawers[position].startsWith("null?")) {
                PrintUtil.printRespones("活动图片地址:" + drawers[position]);
                Glide.with(fragment).load(drawers[position])
//                查看大图时放弃使用内存缓存，图片从网络下载完成后会缓存到磁盘中，加载会从磁盘中加载，这样可以加速内存的回收
//                    .skipMemoryCache(true)
                        .placeholder(R.drawable.one)
                        .error(R.drawable.one)
                        .into(imageView);

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(fragment.getActivity(), FoundActivity.class);
                        intent.putExtra("found", 13);
                        intent.putExtra("realLink", links[position]);
                        intent.putExtra("realId", ids[position]);
                        fragment.startActivity(intent);
                    }
                });
            }
        } else {
            PrintUtil.printRespones("展示默认活动");
            imageView.setImageResource(drawers_noData[position]);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}
